package com.yxplearn.mybatisplus.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther : yxpweb
 * @Date : 2021/8/14 12:41
 * @Description: 关于cookie的测试
 */

@RequestMapping("/cookie")
@Controller
public class CookiesController {

    @RequestMapping("/readServerCookie")
    @ResponseBody
    public String readServerCookie(HttpServletResponse response) {
        // 创建一个 cookie对象
        Cookie cookie = new Cookie("username", "Jovan");
        cookie.setHttpOnly(true);
        cookie.setMaxAge(60);
        //将cookie对象加入response响应
        response.addCookie(cookie);
        return "Username is changed!";
    }

    @RequestMapping("/receiveClientCookie")
    @ResponseBody
    public String receiveClientCookie(@CookieValue(value = "username", defaultValue = "Atta") String username) {
        return "Hey! My username is " + username;
    }

    @RequestMapping("/insertTest1")
    @ResponseBody
    public String insertTest1(@CookieValue(value = "username", defaultValue = "Atta") String username) {
        System.out.println("mybu test");
        return "Hey! My username is " + username;
    }

    @RequestMapping("/insertTest2")
    @ResponseBody
    public String insertTest() {
        return "Hey! My username is ";
    }



}
